<?php
function plugin_render() {
    $result = '';
    $error = '';
    $input = '';
    $key = '';
    $action = 'encrypt';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $input = $_POST['input'] ?? '';
        $key = $_POST['key'] ?? '';
        $action = $_POST['action'] ?? 'encrypt';
        if ($input === '' || $key === '') {
            $error = '请输入内容和密钥';
        } else {
            $method = 'AES-128-CBC';
            $iv = substr(hash('sha256', $key), 0, 16);
            if ($action === 'encrypt') {
                $result = openssl_encrypt($input, $method, $key, 0, $iv);
            } else {
                $result = openssl_decrypt($input, $method, $key, 0, $iv);
            }
        }
    }
    ?>
    <form method="post" class="space-y-4">
      <textarea name="input" rows="3" class="w-full border rounded p-2" placeholder="请输入要加密/解密的内容"><?=htmlspecialchars($input)?></textarea>
      <input type="text" name="key" value="<?=htmlspecialchars($key)?>" class="w-full border rounded p-2" placeholder="密钥">
      <div class="flex gap-4">
        <label><input type="radio" name="action" value="encrypt" <?php if($action==='encrypt')echo'checked';?>> 加密</label>
        <label><input type="radio" name="action" value="decrypt" <?php if($action==='decrypt')echo'checked';?>> 解密</label>
      </div>
      <button type="submit" class="bg-blue-500 text-white px-6 py-2 rounded">执行</button>
      <?php if ($error): ?>
        <div class="text-red-500"><?=htmlspecialchars($error)?></div>
      <?php endif; ?>
    </form>
    <?php if ($result): ?>
      <div class="mt-4">
        <textarea class="w-full h-24 border rounded p-2 text-xs bg-gray-50" readonly><?=htmlspecialchars($result)?></textarea>
      </div>
    <?php endif;
} 